"""
A JSON-based API(view) for the Worde blogging system
"""
__revision__ = "$Id: json.py 7 2007-01-14 11:26:04Z jens.persson $"
from turbojson.jsonify import jsonify

from turbojson.jsonify import jsonify_sqlobject
from worde.model import User, Group, Permission #IGNORE:W0611

@jsonify.when('isinstance(obj, Group)')
def jsonify_group(obj):
    """
    Convert a Group into JSON
    """
    result = jsonify_sqlobject( obj )
    result["users"] = [u.user_name for u in obj.users]
    result["permissions"] = [p.permission_name for p in obj.permissions]
    return result

@jsonify.when('isinstance(obj, User)')
def jsonify_user(obj):
    """
    Convert a User into JSON
    """
    result = jsonify_sqlobject( obj )
    del result['password']
    result["groups"] = [g.group_name for g in obj.groups]
    result["permissions"] = [p.permission_name for p in obj.permissions]
    return result

@jsonify.when('isinstance(obj, Permission)')
def jsonify_permission(obj):
    """
    Convert a Permission into JSON
    """
    result = jsonify_sqlobject( obj )
    result["groups"] = [g.group_name for g in obj.groups]
    return result